﻿
namespace SceneManager
{
    public class HalfByte3
    {
        public int Width { get; private set; }
        public int Height { get; private set; }
        public int Depth { get; private set; }

        private int size;
        private byte[] data;

        public HalfByte3(int width, int height, int depth)
        {
            Width = width;
            Height = height;
            Depth = depth;

            size = Width * Height * Depth;
            size += size % 2;
            data = new byte[size];
        }

        public byte this[int x, int y, int z]
        {
            get
            {
                int i = y * Width * Depth + z * Width + x;
                return (byte)(data[i / 2] >> (4 * (1 - (i % 2))) & 0xF);
            }
            set
            {
                int i = y * Width * Depth + z * Width + x;
                byte b = data[i / 2];
                b &= (byte)(0xF << (4 * (i % 2)));
                b |= (byte)((value & 0xF) << (4 * (1 - (i % 2))));
                data[i / 2] = b;
            }
        }

        public void Fill(byte value)
        {
            value &= 0xF;
            value |= (byte)(value << 4);

            for (int i = 0; i < size; i++)
                data[i] = value;
        }
    }
}
